'use strict';
let vk;
const dbName = {
	//test: "vk-test", 
};

let db = uniCloud.database();
let _ = db.command;
let $ = _.aggregate;

let cloudObject = {
	isCloudObject: true,

	_before: async function() {
		vk = this.vk;
		// let { customUtil, uniID, config, pubFun } = this.getUtil(); // 获取工具包
	},

	_after: async function(options) {
		let {
			err,
			res
		} = options;
		if (err) {
			return;
		}
		return res;
	},
	toExamine: async function(data) {
		let res = {
			code: 0,
			msg: ''
		};

		// false = 不显示 ， true = 显示
		res.data = true
		return res
	},

	savecar: async function(data) {
		let res = {
			code: 0,
			msg: '',
			needUpdateUserInfo: true,
		};

		let id = await vk.baseDao.add({
			dbName: "carList", // 表名
			dataJson: { // 需要新增的数据 json格式
				_id: data._id,
				phoneNumber: data.phoneNumber,
				content: data.content,
				value: data.value
			}
		});

		res.id = id
		res.msg = '提交成功'
		return res;
	},
	findcar: async function(data) {
		let res = {
			code: 0,
			msg: '',
		};

		let info = await vk.baseDao.findById({
			dbName: "carList",
			id: data._id
		});

		res.data = info
		res.msg = '查询成功'
		return res;
	},
	findcarNumber: async function(data) {
		let res = {
			code: 0,
			msg: '',
		};

		let info = await vk.baseDao.findByWhereJson({
			dbName: "carList",
			whereJson: {
				value: data.value,
			}
		});
		res.data = info
		res.msg = ''
		return res;
	},
	updatacar: async function(data) {
		let res = {
			code: 0,
			msg: '',
		};

		let newInfo = await vk.baseDao.updateById({
			dbName: "carList",
			id: data._id,
			dataJson: {
				phoneNumber: data.phoneNumber,
				content: data.content,
				value: data.value,
			},
			getUpdateData: true, // 去掉getUpdateData，则不会返回修改后的数据对象
		});

		res.data = newInfo
		res.msg = '更新成功'
		return res;
	},

};

module.exports = cloudObject;